package Page.BookBorrowingRecordPage;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.DatePicker;
import javafx.scene.control.TextField;
import javafx.scene.text.Text;

import java.net.URL;
import java.util.ResourceBundle;

/**
 * @since 2024-06-02
 * @author yooo_fan
 */
public class AddRecordController implements Initializable {
    @FXML
    public TextField InputPeopleName;
    @FXML
    public Text Title;
    @FXML
    public TextField InputBookId;
    @FXML
    public TextField InputBookName;
    @FXML
    public Button CheckBook;
    @FXML
    public DatePicker InputData;
    @FXML
    public Button ConfirmButton;

    private AddRecordViewModel viewModel;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        viewModel = new AddRecordViewModel();

        InputPeopleName.textProperty().bindBidirectional(viewModel.getInputPeopleName());
        InputBookId.textProperty().bindBidirectional(viewModel.getInputBookId());
        InputBookName.textProperty().bindBidirectional(viewModel.getInputBookName());

        InputPeopleName.textProperty().addListener((observable, oldValue, newValue) -> {
            viewModel.updateInputPeopleName(newValue);
        });
        InputBookName.textProperty().addListener((observable, oldValue, newValue) -> {
            viewModel.updateInputBookName(newValue);
        });
        InputBookId.textProperty().addListener((observable, oldValue, newValue) -> {
            viewModel.updateInputBookId(newValue);
        });

        ConfirmButton.disableProperty().bindBidirectional(viewModel.getIfOkStatus());
    }

    public void confirmButtonClicked(ActionEvent actionEvent) {
        viewModel.clickConfirmButton();
    }

    public void checkButtonClicked(ActionEvent actionEvent) {
        viewModel.clickCheckButton();
    }
}
